home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / common / blockmem.cpp < prev    next >
C/C++ Source or Header  |  2002-02-25  |  4KB  |  159 lines

  1.  
  2. /// ********* WIN32 **********
  3.  
  4. #ifdef SYSTEM_WIN32
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #include <malloc.h>
  8. #include "cmdlib.h"
  9. #include "messages.h"
  10. #include "log.h"
  11. #include "hlassert.h"
  12. #include "blockmem.h"
  13.  
  14. // =====================================================================================
  15. //  AllocBlock
  16. // =====================================================================================
  17. void*           AllocBlock(const unsigned long size)
  18. {
  19.     void*           pointer;
  20.     HANDLE          h;
  21.  
  22.     if (!size)
  23.     {
  24.         Warning("Attempting to allocate 0 bytes");
  25.     }
  26.  
  27.     h = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, size);
  28.  
  29.     if (h)
  30.     {
  31.         pointer = GlobalLock(h);
  32.     }
  33.     else
  34.     {
  35.         return NULL;
  36.     }
  37.  
  38.     return pointer;
  39. }
  40.  
  41. // =====================================================================================
  42. //  FreeBlock
  43. // =====================================================================================
  44. bool            FreeBlock(void* pointer)
  45. {
  46.     HANDLE          h;
  47.  
  48.     if (!pointer)
  49.     {
  50.         Warning("Freeing a null pointer");
  51.     }
  52.  
  53.     h = GlobalHandle(pointer);
  54.  
  55.     if (h)
  56.     {
  57.         GlobalUnlock(h);
  58.         GlobalFree(h);
  59.         return true;
  60.     }
  61.     else
  62.     {
  63.         Warning("Could not translate pointer into handle");
  64.         return false;
  65.     }
  66. }
  67.  
  68. #ifdef CHECK_HEAP
  69. // =====================================================================================
  70. //  HeapCheck
  71. // =====================================================================================
  72. void            HeapCheck()
  73. {
  74.     if (_heapchk() != _HEAPOK)
  75.         hlassert(false);
  76. }
  77. #endif
  78.  
  79. // =====================================================================================
  80. //  AllocBlock
  81. // =====================================================================================
  82. // HeapAlloc/HeapFree is thread safe by default
  83. void*           Alloc(const unsigned long size)
  84. {
  85.     HeapCheck();
  86.     return calloc(1, size);
  87. }
  88.  
  89. // =====================================================================================
  90. //  AllocBlock
  91. // =====================================================================================
  92. bool            Free(void* pointer)
  93. {
  94.     HeapCheck();
  95.     free(pointer);
  96.     return true;
  97. }
  98.  
  99. #endif /// ********* WIN32 **********
  100.  
  101.  
  102.  
  103.  
  104. /// ********* POSIX **********
  105.  
  106. #ifdef SYSTEM_POSIX
  107. #ifdef HAVE_CONFIG_H
  108. #include "config.h"
  109. #endif
  110. #ifdef STDC_HEADERS
  111. #include <stdlib.h>
  112. #endif
  113. #include "cmdlib.h"
  114. #include "messages.h"
  115. #include "log.h"
  116.  
  117. // =====================================================================================
  118. //  AllocBlock
  119. // =====================================================================================
  120. void*           AllocBlock(const unsigned long size)
  121. {
  122.     if (!size)
  123.     {
  124.         Warning("Attempting to allocate 0 bytes");
  125.     }
  126.     return calloc(1, size);
  127. }
  128.  
  129. // =====================================================================================
  130. //  FreeBlock
  131. // =====================================================================================
  132. bool            FreeBlock(void* pointer)
  133. {
  134.     if (!pointer)
  135.     {
  136.         Warning("Freeing a null pointer");
  137.     }
  138.     free(pointer);
  139.     return true;
  140. }
  141.  
  142. // =====================================================================================
  143. //  Alloc
  144. // =====================================================================================
  145. void*           Alloc(const unsigned long size)
  146. {
  147.     return AllocBlock(size);
  148. }
  149.  
  150. // =====================================================================================
  151. //  Free
  152. // =====================================================================================
  153. bool            Free(void* pointer)
  154. {
  155.     return FreeBlock(pointer);
  156. }
  157.  
  158. #endif /// ********* POSIX **********
  159.